{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f597e714",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "api_key = \"dAyJdlVlkTKc2PHaDc_Qoc-5NXTWZEx-\"\n",
    "api_secret = \"ue8dzp1KefHMSLYWXP3ExZLKELfoBOEp\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "78334fb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 87,\n",
       " 'error_message': 'FACESET_EXIST',\n",
       " 'request_id': '1648476984,0efe866a-7247-46a4-8594-1b2389c1714a'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建人脸集合\n",
    "create_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "    \"display_name\": \"西7宿舍\",\n",
    "    \"outer_id\": \"4人\",\n",
    "}\n",
    "r = requests.post(create_url, params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "461839d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 82,\n",
       " 'facesets': [{'faceset_token': '0387d9794143c1e290e34a7110138c66',\n",
       "   'outer_id': 'face09',\n",
       "   'display_name': '人脸集合',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'e550db127ad0b3ec90934058cbcaeba9',\n",
       "   'outer_id': 'face001',\n",
       "   'display_name': '人脸集合',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '3daacf6c5f257c4e8d7c5272b13a27f6',\n",
       "   'outer_id': 'face002',\n",
       "   'display_name': '人脸集合',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '9c7339b73d8080bcf56c374eb3546c67',\n",
       "   'outer_id': '001',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'f4c36e3389476bf0fb3729e00eb0da41',\n",
       "   'outer_id': '01',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '6c8f884e25cb81a58e583dac00ff3e90',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '8d7b9be6440d2ed3fbd4098783a85048',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '55ee9efde9bd2cf654b28b35128712f7',\n",
       "   'outer_id': '4人',\n",
       "   'display_name': '西7宿舍',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648476984,aef9110c-0ff6-4d71-b246-a775cfa02036'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取人脸库列表及信息\n",
    "getFaces_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "}\n",
    "\n",
    "r = requests.post(getFaces_url, payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8427d744",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'55ee9efde9bd2cf654b28b35128712f7'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取 faceset_token\n",
    "faceset_token = r.json()[\"facesets\"][-1][\"faceset_token\"]\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7b067a3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '55ee9efde9bd2cf654b28b35128712f7',\n",
       " 'request_id': '1648476985,f23c7cc7-702d-49a9-939a-2ff5199946f0',\n",
       " 'time_used': 83,\n",
       " 'outer_id': '4人'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 更新信息\n",
    "update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "    \"faceset_token\": faceset_token,\n",
    "    \"user_data\": \"101\"\n",
    "}\n",
    "\n",
    "r = requests.post(update_url, payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5734887c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '55ee9efde9bd2cf654b28b35128712f7',\n",
       " 'tags': '',\n",
       " 'time_used': 95,\n",
       " 'user_data': '101',\n",
       " 'display_name': '西7宿舍',\n",
       " 'face_tokens': ['565a7697e952d8369baaff3cb3b488ba',\n",
       "  '87cd113d3d1984dc434fe7533be7a251',\n",
       "  '0e3058de9e160c3fafe36a2040b46ecd',\n",
       "  '604e85db2ffa351e39dc5f73dbb984dd',\n",
       "  '4d1bc8d7482330793a370fd0beed4d0a',\n",
       "  'f6934b6adc9ff093fb08894b4eb22349',\n",
       "  'cc70ee3a64ef919f51c50b9d5c708a38',\n",
       "  '7a6164df95b4924b1dadab6955b108c7'],\n",
       " 'face_count': 8,\n",
       " 'request_id': '1648476985,239d4b4e-9e83-4268-983a-8fd277bfd92c',\n",
       " 'outer_id': '4人'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查数据集合\n",
    "getDetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "    \"faceset_token\": faceset_token,\n",
    "}\n",
    "\n",
    "r = requests.post(getDetail_url, payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8cca3a1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 人脸检测，人脸添加，人脸分析，人脸对比，人脸搜索\n",
    "import detect, addface, compare, analyze, search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b82c3c0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'f8c25b8e9f077e4377b5e204269bbef2'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第一张图片 token\n",
    "first = detect.face_detect(api_key,api_secret,\"1.jpg\")\n",
    "face01_token = first[\"faces\"][0][\"face_token\"]\n",
    "face01_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e27a6897",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '55ee9efde9bd2cf654b28b35128712f7',\n",
       " 'time_used': 537,\n",
       " 'face_count': 9,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648476986,2e2e9cb0-557c-466d-8506-959698fbace1',\n",
       " 'outer_id': '4人',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将人脸添加到集合\n",
    "addface.addFace(api_key, api_secret,faceset_token, face01_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4c13409c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'d4457156695b78579c67e7da18f26cc0'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二张图片 token\n",
    "second = detect.face_detect(api_key,api_secret,\"2.jpg\")\n",
    "face02_token = second[\"faces\"][0][\"face_token\"]\n",
    "face02_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "90158705",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '55ee9efde9bd2cf654b28b35128712f7',\n",
       " 'time_used': 505,\n",
       " 'face_count': 10,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648476987,7acfcd4c-5db1-4164-8d11-f78d6d30e5e2',\n",
       " 'outer_id': '4人',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将人脸添加到集合\n",
    "addface.addFace(api_key, api_secret,faceset_token, face02_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2b6deafe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c3539079fb334821e56b5a348db9a208'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第三张图片 token\n",
    "third = detect.face_detect(api_key,api_secret,\"3.jpg\")\n",
    "face03_token = third[\"faces\"][0][\"face_token\"]\n",
    "face03_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9b30dddd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '55ee9efde9bd2cf654b28b35128712f7',\n",
       " 'time_used': 462,\n",
       " 'face_count': 11,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648476988,abfcbd62-9873-46a3-9624-ea8da9f11702',\n",
       " 'outer_id': '4人',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将人脸添加到集合\n",
    "addface.addFace(api_key, api_secret,faceset_token, face03_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cda20ce8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'17115fae0a728d01df7b434acfb78772'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第四张图片 token\n",
    "fourth = detect.face_detect(api_key,api_secret,\"4.jpg\")\n",
    "face04_token = fourth[\"faces\"][0][\"face_token\"]\n",
    "face04_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f1ca25fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '55ee9efde9bd2cf654b28b35128712f7',\n",
       " 'time_used': 436,\n",
       " 'face_count': 12,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648476990,65f30074-99f6-4147-808a-0e2f0030afb5',\n",
       " 'outer_id': '4人',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将人脸添加到集合\n",
    "addface.addFace(api_key, api_secret,faceset_token, face04_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dfb84535",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'955e591921463f047bb001af2a343ab5'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 人脸搜索\n",
    "# 准备一张新的照片\n",
    "fifth = detect.face_detect(api_key,api_secret,\"5.jpg\")\n",
    "face05_token = fifth[\"faces\"][0][\"face_token\"]\n",
    "face05_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "059bfbb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648476991,97ea8100-b0bc-4cc9-928a-2c4a85b923a0',\n",
       " 'time_used': 367,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 71.203,\n",
       "   'user_id': '',\n",
       "   'face_token': 'c3539079fb334821e56b5a348db9a208'}]}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 搜索\n",
    "search.face_search(api_key, api_secret, face05_token, faceset_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "67aadef9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648477371,6a3cd983-1f93-410f-8fad-f68c32a22855',\n",
       " 'time_used': 368,\n",
       " 'confidence': 50.19,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 然后进行对比\n",
    "compareFace = compare.face_compare(api_key, api_secret,face01_token,face05_token)\n",
    "compareFace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "9fbfd90a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不是本宿舍成员\n"
     ]
    }
   ],
   "source": [
    "# 最后判断是否为本宿舍成员\n",
    "if compareFace['confidence'] >= compareFace['thresholds']['1e-5']:\n",
    "    print(\"是本宿舍成员\")\n",
    "else:\n",
    "    print(\"不是本宿舍成员\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9c9d5027",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b7c420c5bae11e3386ffc5c30f4f8098'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 再准备一张新的照片\n",
    "sixth = detect.face_detect(api_key,api_secret,\"6.jpg\")\n",
    "face06_token = sixth[\"faces\"][0][\"face_token\"]\n",
    "face06_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "2acf3064",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648477454,fb2c8e77-5fad-4d58-a887-182ab749513b',\n",
       " 'time_used': 264,\n",
       " 'confidence': 91.049,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 然后进行对比\n",
    "compareFace1 = compare.face_compare(api_key, api_secret,face06_token,face03_token)\n",
    "compareFace1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "84855e17",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是本宿舍成员\n"
     ]
    }
   ],
   "source": [
    "# 最后判断是否为本宿舍成员\n",
    "if compareFace1['confidence'] >= compareFace1['thresholds']['1e-5']:\n",
    "    print(\"是本宿舍成员\")\n",
    "else:\n",
    "    print(\"不是本宿舍成员\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "177ae126",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 198,\n",
       " 'request_id': '1648477823,0e4cb373-21a5-45d6-85ef-8a8ee212ec7c',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.003,\n",
       "     'neutral': 99.754,\n",
       "     'disgust': 0.002,\n",
       "     'anger': 0.005,\n",
       "     'surprise': 0.072,\n",
       "     'fear': 0.006,\n",
       "     'happiness': 0.158},\n",
       "    'beauty': {'female_score': 90.727, 'male_score': 89.288},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 20},\n",
       "    'mouthstatus': {'close': 39.453,\n",
       "     'surgical_mask_or_respirator': 0.001,\n",
       "     'open': 60.545,\n",
       "     'other_occlusion': 0.001},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 2.541,\n",
       "     'stain': 12.063,\n",
       "     'acne': 4.598,\n",
       "     'health': 77.258},\n",
       "    'headpose': {'yaw_angle': 20.454609,\n",
       "     'pitch_angle': 8.704714,\n",
       "     'roll_angle': -6.055829},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.775},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.775},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.775}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.132},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.067,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.932,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.306,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.001,\n",
       "      'no_glass_eye_open': 99.692,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.001}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 13.703},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.538,\n",
       "      'vector_z_component': 0.946,\n",
       "      'vector_x_component': 0.155,\n",
       "      'vector_y_component': -0.284,\n",
       "      'position_y_coordinate': 0.342},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.57,\n",
       "      'vector_z_component': 0.936,\n",
       "      'vector_x_component': 0.269,\n",
       "      'vector_y_component': -0.225,\n",
       "      'position_y_coordinate': 0.376}}},\n",
       "   'face_rectangle': {'width': 159, 'top': 447, 'left': 521, 'height': 159},\n",
       "   'face_token': 'b7c420c5bae11e3386ffc5c30f4f8098'}]}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 也可以对图片进行一下分析\n",
    "analyze.face_analyze(api_key, api_secret, face06_token, \"gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9bd0aa4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
